/**!
 * 常见问题
 * author: wangkai;
 */
$(function() {
    var $problemFrom = $("#problemFrom"),
        $pager = $(".pager"),
        $txtsearch = $("#txtsearch"),
        $btnsearch = $("#btnsearch");
    var problemFromTmpl = Util.clearHtml($("#problemFrom-tmpl").html());
    var M = Mustache;
    M.parse(problemFromTmpl);
    //左侧
    function tableData(pageindex, pagesize) {
        var param = {
            pageIndex: pageindex + 1,
            pageSize: pagesize,
            replyDepartGuid: ''
        };
        //问题库类别（回复部门唯一标识）
        var pdeptguid = Util.getUrlParams("departGuid");
        if (pdeptguid != undefined && pdeptguid != "") {
            param.replyDepartGuid = pdeptguid;
        }
        if ($.trim($txtsearch.val()) != "") { //关键字
            param.condition = $txtsearch.val();
        } else {
            param.condition = "";
        }
        //类别 ，1是咨询，2是建议，3是投诉，4是求助，5是全部
        param.appealType = $("#testSelect").get(0).selectedIndex + 1;
        /*  Util.ajax({
                url: 'commonProblem',
                type: 'POST',
                dataType: 'json',
                data: param
            })
            .done(function(data) {
                console.log(data.problemInfo);
                renderList(data.problemInfo);

                renderPager(pageindex, pagesize, data.totalNum);
            })
            .fail(function() {
                console.log("error");
            });  */
        $.ajax({
            url: Util.AjaxRUrl+'commonProblem',
            type: "post",
            dataType: "json",
            data: {
                param: '{appealType:"' + param.appealType + '",condition:"' + param.condition + '",replyDepartGuid:"' + param.replyDepartGuid + '",pageIndex:"' + param.pageIndex + '",pageSize:"' + param.pageSize + '"}'
            },
            success: function(data) {
                if (data.problemInfo) {
                    var problemInfo = data.problemInfo;
                    for (var i = 0; i < problemInfo.length; i++) {
                        if (problemInfo[i].appealType == "1") {
                            problemInfo[i].appealType = "咨询";
                        } else if (problemInfo[i].appealType == "2") {
                            problemInfo[i].appealType = "建议";
                        } else if (problemInfo[i].appealType == "3") {
                            problemInfo[i].appealType = "投诉";
                        } else if (problemInfo[i].appealType == "4") {
                            problemInfo[i].appealType = "问题";
                        }
                    }
                    //console.log('/apis/hfesewb/rest/hfService/commonProblem?param={"appealType":"' + param.appealType + '","condition":"' + param.condition + '","replyDepartGuid":"' + param.replyDepartGuid + '","pageIndex":"' + pageindex + '","pageSize":"' + pagesize + '"}');
                    renderList(data.problemInfo);
                    renderPager(pageindex, param.pagesize, data.totalNum);
                }

            },
            error: function(XMLHttpRequest, txtStatus, errorThrown) {
                console.log(XMLHttpRequest);
            }
        });
    }

    function renderList(data) {
        $problemFrom.html(M.render(problemFromTmpl, { items: data }));
        $("#problemFrom").find("tr").eq(0).addClass("top");
    }

    function renderPager(pageindex, pagesize, total) {
        if ($pager.pagination()) {
            $pager.pagination('destroy');
        }
        $pager.pagination({
            pageIndex: pageindex,
            pageSize: pagesize,
            total: total,
            showJump: true,
            jumpBtnText: 'Go'
        });
        $pager.on("pageClicked", function(event, data) {
            //分页按钮点击事件
            console.log(data);
            tableData(data.pageIndex, data.pageSize);
        });
        $pager.on("jumpClicked", function(event, data) {
            //跳转按钮点击事件
            tableData(data.pageIndex, data.pageSize);
        });
    }
    //首次加载
    tableData(0, 15);
    //搜索
    $btnsearch.on('click', function(event) {
        tableData(0, 15);
    });
});
